package be.kuleuven.mume.paginas;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.Vak;
import be.kuleuven.mume.framework.Vraag;

public class VraagEditPagina extends Activity implements OnClickListener{
	private TextView mTitleText;
	private EditText mSubjectText;
	private EditText mBodyText;
	private Vraag vraag;
	private String vakId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	    setContentView(R.layout.vraag_edit);
	    
	    if ( customTitleSupported ) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
        }
	    
	    Bundle extras = getIntent().getExtras();
	    vakId = extras.getString(Vak.KEY_ID);
	    vraag = (Vraag)extras.get(Vraag.VRAAG);
	    if(vraag == null) {
	    	vraag = new Vraag();
	    	vraag.setVakId(vakId);
	    }
	    
	    mTitleText = (TextView) findViewById(R.id.myTitle);
        if ( mTitleText != null ) {
        	mTitleText.setText(extras.getString(Vak.KEY_NAAM));
        }

        mSubjectText = (EditText) findViewById(R.id.subject);
        mSubjectText.setText(vraag.getSubject());
	    mBodyText = (EditText) findViewById(R.id.body);
	    mBodyText.setText(vraag.getText());

	    Button confirmButton = (Button) findViewById(R.id.confirm);

	    confirmButton.setOnClickListener(this);
	}
	
    public void onClick(View view) {
        Bundle extras = new Bundle();
        
        vraag.setSubject(mSubjectText.getText().toString());
        vraag.setText(mBodyText.getText().toString());
        
        extras.putSerializable(Vraag.VRAAG, vraag);

        Intent mIntent = new Intent();
        mIntent.putExtras(extras);
        setResult(RESULT_OK, mIntent);
        finish();
    }
}
